package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.mapper.RoadMapper;
import com.woniu.pojo.Road;
import com.woniu.pojo.Space;
import com.woniu.mapper.SpaceMapper;
import com.woniu.service.IRoadService;
import com.woniu.service.ISpaceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.vo.SpaceQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static java.util.stream.Collectors.toList;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lqg
 * @since 2022-10-12
 */
@Service
public class SpaceServiceImpl extends ServiceImpl<SpaceMapper, Space> implements ISpaceService {

    @Resource
    private SpaceMapper spaceMapper;

    @Autowired
    private IRoadService roadService;

    @Resource
    private RoadMapper roadMapper;

    /**
     * 分页条件查询
     * @param currentPage    当前页
     * @param pageSize       每页记录数
     * @param spaceQueryVo   泊位查询vo
     * @return               分页查询数据
     */
    @Override
    public IPage<Space> search(Integer currentPage, Integer pageSize, SpaceQueryVo spaceQueryVo) {
        // 0. 空值检测
        if (currentPage == null || currentPage < 1) {
            // 若currentPage 传入空值或小于一则令其等于1
            currentPage = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 5;
        }
        // 1. 创建分页
        IPage<Space> page = new Page<>(currentPage, pageSize);

        // 2. 创建wrapper
        QueryWrapper<Space> spaceQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Road> roadQueryWrapper = new QueryWrapper<>();
        // 3. 检测查询条件
        if (spaceQueryVo != null) {
            if (spaceQueryVo.getRoadName() != null && spaceQueryVo.getRoadName().length() > 0) {
                // 路段名存在 查询路段id
                roadQueryWrapper.like("name", spaceQueryVo.getRoadName());
            }

            if (spaceQueryVo.getRoadType() != null && spaceQueryVo.getRoadType().length() > 0) {
                // 路段类型存在 查询路段id
                roadQueryWrapper.eq("type", spaceQueryVo.getRoadType());
            }
            // 根据前两个条件 查询roads
            List<Road> roads = roadMapper.selectList(roadQueryWrapper);
            // 将roads中的id保存到list
            List<Long> roadsId = new ArrayList<>();
            for (Road road : roads) {
                roadsId.add(road.getId());
            }


            // 对roads 控制检测
            if (roadsId.size() == 0) {
                return null;
            }


            if (spaceQueryVo.getPatrolName() != null && spaceQueryVo.getPatrolName().length() > 0) {
                // 巡检员名称存在 查询路段id
                // 根据巡检员姓名查询巡检员id
                List<Long> patrolIdList = roadMapper.selectPatrolIdsByPatrolName(spaceQueryVo.getPatrolName());
                // 建立路段id的list
                List<Long> roadIdList = new ArrayList<>();
                // 对patrolIdList迭代 根据巡检员id查询路段id
                for (Long patrolId : patrolIdList) {
                    List<Long> roadIds = roadMapper.selectRoadIdsByPatrolId(patrolId);
                    // 将roadIds元素添加到roadIdList
                    roadIdList.addAll(roadIds);
                }

                // 对roadIdList进行空值检测
                if (roadIdList.size() > 0) {
                    // 对roadIdList   与roads集合 取交集
                    roadIdList.retainAll(roadsId);


                } else {
                    return null;
                }

                // 对筛选后的roads空值检测
                if (roadIdList.size() > 0) {

                    // 将roads里的id添加到条件中
                    for (Long id : roadIdList) {
                        spaceQueryWrapper.eq("road_id", id).or();
                    }

                } else {
                    return null;
                }
            } else {
                // 将roads中的id添加到条件中
                for (Road road : roads) {
                    System.out.println(road.getId());
                    spaceQueryWrapper.eq("road_id", road.getId()).or();
                }
            }
        }


        return spaceMapper.selectPage(page, spaceQueryWrapper);
    }
}
